import java.util.HashSet;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-29
 * Time:15:33
 */

/**
 * 力扣 2423.删除字符使频率相同
 */

public class equalFrequency {
    public static void main(String[] args) {
        System.out.println(equalFrequency("cccaa"));
    }

    //    public static boolean equalFrequency(String word) {
//        int j = 1;
//        int count = 0;
//        for (int i = 0; i < word.length() - 2 || j <= word.length() - 1; i++,j++) {
//            if (word.charAt(i) == word.charAt(j)){
//                count++;
//                if ((i + 2) < word.length() && (word.charAt(i) == word.charAt(i + 2))){
//                    return false;
//                }
//            }
//
//        }
//        return  count <= 1 ? true : false;
//    }
    public static boolean equalFrequency(String word) {
        int[] charCount = new int[26];
        int n = word.length();
        for (int i = 0; i < n; i++) {
            charCount[word.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (charCount[i] == 0) {
                continue;
            }
            charCount[i]--;
            HashSet<Integer> frequency = new HashSet<Integer>();
            for (int f : charCount) {
                if (f > 0) {
                    frequency.add(f);
                }
            }
            if (frequency.size() == 1) {
                return true;
            }
            charCount[i]++;
        }
        return false;
    }
}
